<html>
<head><meta charset="utf-8"><title>Remove matching pair of chars in Vec&lt;char&gt; · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html">Remove matching pair of chars in Vec&lt;char&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236534839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236534839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236534839">(Apr 28 2021 at 16:09)</a>:</h4>
<p>So I'm doing Advent of Code 2018 Day 5 and to solve it I need to remove matching pairs of char (Aa and aA, not aa or AA). I have the following:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">calculate_polymer</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">input</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">().</span><span class="n">saturating_sub</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">]);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">upper_lower</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">y</span><span class="p">.</span><span class="n">to_ascii_uppercase</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">to_ascii_lowercase</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">y</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">lower_upper</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">to_ascii_uppercase</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">y</span><span class="p">.</span><span class="n">to_ascii_lowercase</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">upper_lower</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">lower_upper</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">input</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">input</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Is there a less verbose way to write this? Using <code>.windows(2)</code> doesn't seem to work quite right because I need to remove the chars while comparing. Peekable + filter?</p>



<a name="236557571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236557571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236557571">(Apr 28 2021 at 18:41)</a>:</h4>
<p>I'm not sure exactly what your asking about, there is <a href="https://lib.rs/crates/arrayref">https://lib.rs/crates/arrayref</a></p>



<a name="236580521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236580521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236580521">(Apr 28 2021 at 21:12)</a>:</h4>
<p>I'm wondering if anyone knows of a better / more concise solution to this problem. I want all pairs of characters in a <code>Vec&lt;char&gt;</code> that are either <code>Aa / aA</code>, <code>Bb / bB</code> etc.. removed.</p>



<a name="236581581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236581581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236581581">(Apr 28 2021 at 21:16)</a>:</h4>
<p>the method signature looks like you don't need to remove them, only count how much remains after skipping them</p>



<a name="236582776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236582776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236582776">(Apr 28 2021 at 21:22)</a>:</h4>
<p>I'm confused as to what the requirements are. If I'm understanding your description correctly, <code>aAbBc</code> should return 1, but it returns 3.</p>



<a name="236585545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236585545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236585545">(Apr 28 2021 at 21:41)</a>:</h4>
<p><code>aAbBc</code> should return 1 yeah, I'm currently running the function recursively when input changed, otherwise return <code>.count()</code>.</p>



<a name="236585630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236585630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236585630">(Apr 28 2021 at 21:42)</a>:</h4>
<p>For instance, <code>dabAcCaCBAcCcaDA</code> should become <code>dabCBAcaDA</code></p>



<a name="236585773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236585773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236585773">(Apr 28 2021 at 21:43)</a>:</h4>
<p>ah, yeah, makes sense. I was weirded out by the way you increment <code>i</code> by two when you remove a pair, even though you already skipped those positions by removing them</p>



<a name="236585857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236585857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236585857">(Apr 28 2021 at 21:44)</a>:</h4>
<p>I've found this nugget thus far, although it doesn't help with too much:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">opposites</span><span class="p">(</span><span class="n">x</span>: <span class="kt">char</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">char</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">32</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236587470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236587470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236587470">(Apr 28 2021 at 22:00)</a>:</h4>
<p>Your suggestion of weird <code>i</code> increments led me to the following solution, thanks a bunch.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">opposites</span><span class="p">(</span><span class="n">x</span>: <span class="kt">char</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">char</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">32</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">calculate_polymer</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">input</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">().</span><span class="n">saturating_sub</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">opposites</span><span class="p">(</span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">input</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">input</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">input</span><span class="p">.</span><span class="n">remove</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">saturating_sub</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236598465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236598465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Julián Espina Del Ángel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236598465">(Apr 28 2021 at 23:59)</a>:</h4>
<p>Made a version using only iterators just for fun. Please don't do this to yourself.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">fs</span>::<span class="n">read_to_string</span><span class="p">(</span><span class="s">"input.txt"</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">idxs</span>: <span class="nc">HashSet</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">input</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">chars</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">zip</span><span class="p">(</span><span class="n">input</span><span class="p">.</span><span class="n">chars</span><span class="p">().</span><span class="n">skip</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="n">chain</span><span class="p">(</span><span class="n">once</span><span class="p">(</span><span class="sc">'\0'</span><span class="p">)))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">enumerate</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter_map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">))</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u8</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">Some</span><span class="p">(</span><span class="fm">vec!</span><span class="p">[</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">])</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">None</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">fold</span><span class="p">(</span><span class="n">HashSet</span>::<span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span>::<span class="n">new</span><span class="p">(),</span><span class="w"> </span><span class="o">|</span><span class="k">mut</span><span class="w"> </span><span class="n">acc</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">acc</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="o">&amp;</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">acc</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">acc</span><span class="p">.</span><span class="n">extend</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">iter</span><span class="p">());</span><span class="w"></span>
<span class="w">                </span><span class="n">acc</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">result</span>: <span class="nb">String</span> <span class="o">=</span><span class="w"> </span><span class="n">input</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">char_indices</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter_map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">idxs</span><span class="p">.</span><span class="n">contains</span><span class="p">(</span><span class="o">&amp;</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">result</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">input</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">result</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>
</code></pre></div>



<a name="236599217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236599217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236599217">(Apr 29 2021 at 00:06)</a>:</h4>
<p>I <a href="https://github.com/shepmaster/advent-of-code-2018/blob/master/day-5/src/main.rs#L33-L53">solved this previously</a> as</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">react</span><span class="p">(</span><span class="n">polymer</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="n">polymer</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">i</span><span class="p">),</span><span class="w"> </span><span class="n">polymer</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">b</span><span class="p">))</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">opposing_polarity</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">next</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">i</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">break</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="n">next</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">opposing_polarity</span><span class="p">(</span><span class="n">a</span>: <span class="kt">char</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">char</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="n">eq_ignore_ascii_case</span><span class="p">(</span><span class="o">&amp;</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236599361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Remove%20matching%20pair%20of%20chars%20in%20Vec%3Cchar%3E/near/236599361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Remove.20matching.20pair.20of.20chars.20in.20Vec.3Cchar.3E.html#236599361">(Apr 29 2021 at 00:08)</a>:</h4>
<p>looking at it now, that probably should take <code>&amp;[char]</code> as the argument</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>